﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WF_Office_Verwaltung
{
    public partial class frmFileUpload : Form
    {
        nachrichten_klasse aNachrichtenKlasse = new nachrichten_klasse();
        dbCon_klasse aDbconKlasse = new dbCon_klasse();
        string Art = "";
        int Akt_Kunde = -1;
        public frmFileUpload(string UploadArt, int AktKunde)
        {
            InitializeComponent();
            Art = UploadArt;
            Akt_Kunde = AktKunde;
            this.Text = UploadArt + " Hochladen";
            lblDatei.Text = UploadArt + ":";
            if (UploadArt.ToLower() == "jpg")
            {
                lblRechnugsDatum.Text = "Aufnahmedatum";
            }
            else if(UploadArt.ToLower() == "txt")
            {
                lblRechnugsDatum.Text = "Datum";
            }
            else if (UploadArt.ToLower() == "pdf")
            {
                lblRechnugsDatum.Text = "Rechnungsdatum";
            }

        }
        string fullPath = "";
        string fileName = "";
        bool? pdf = false;
        private void btnDurchsuchen_Click(object sender, EventArgs e)
        {
            try
            {
                if (Art.ToLower() == "pdf")
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "PDF-Dateien | *.pdf";
                    ofd.InitialDirectory = @"C:\";
                    DialogResult result = ofd.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        try
                        {
                            fullPath = ofd.FileName;
                            fileName = ofd.SafeFileName;
                            txtDatei.Text = fileName;
                            pdf = true;
                            if (txtDatei.Text != "")
                            {
                                btnUpload.Enabled = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            aNachrichtenKlasse.ErrorMessage("FileUpload: btnDurchsuchen_Click\nTryCatch - PDF", ex);
                        }
                    }
                }
                else if (Art.ToLower() == "jpg")
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Bild-Dateien | *.jpg";
                    ofd.InitialDirectory = @"C:\";
                    DialogResult result = ofd.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        try
                        {
                            fullPath = ofd.FileName;
                            fileName = ofd.SafeFileName;
                            txtDatei.Text = fileName;
                            pdf = false;
                            if (txtDatei.Text != "")
                            {
                                btnUpload.Enabled = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            aNachrichtenKlasse.ErrorMessage("FileUpload: btnDurchsuchen_Click\nTryCatch - JPG", ex);
                        }
                    }
                }
                else if (Art.ToLower() == "txt")
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Text-Datei (*.doc, *.docx, *.pdf, *.txt)|*.doc;*.docx;*.pdf;*.txt";
                    ofd.InitialDirectory = @"C:\";
                    DialogResult result = ofd.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        try
                        {
                            fullPath = ofd.FileName;
                            fileName = ofd.SafeFileName;
                            txtDatei.Text = fileName;
                            pdf = null;
                            if (txtDatei.Text != "")
                            {
                                btnUpload.Enabled = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            aNachrichtenKlasse.ErrorMessage("FileUpload: btnDurchsuchen_Click\nTryCatch - TXT", ex);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage("FileUpload: btnDurchsuchen_Click\n", ex);
            }
        }
        private void btnUpload_Click(object sender, EventArgs e)
        {
            string test = dtpFileUpload.Text;
            DateTime date = dtpFileUpload.Value;
            FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(fs);
            byte[] fileData = reader.ReadBytes((int)fs.Length);
            reader.Close();
            fs.Close();
            SqlConnection conn = aDbconKlasse.getConnection();
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);
            }
            SqlCommand command = aDbconKlasse.getCommandInsertBLOB_OBJECT(fileData, fileName, Akt_Kunde, date, pdf, conn);
            try
            {
                command.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Upload erfolgreich");
                this.Close();
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage("FileUpload: btnUpload_Click", ex);
            }

        }
    }
}
